﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using XsqTech.SmartOA.Dtos.DTO;
using XsqTech.SmartOA.Infrastructure.JWT;
using XsqTech.SmartOA.IService;
using XsqTech.SmartOA.Service.DTO;

namespace XsqTech.SmartOA.Application.Controllers
{
    /// <summary>
    /// 用户控制器
    /// </summary>
    [Route("api/[controller]")]//路由头
    [ApiController]
    [Authorize]//默认加授权锁
    public class UsersController : ControllerBase
    {
        IUserService _userService;
        readonly ITokenHelper _tokenHelper;
        /// <summary>
        /// 构造函数
        /// </summary>
        public UsersController(IUserService userService,ITokenHelper tokenHelper)
        {
            _userService = userService;
            _tokenHelper = tokenHelper;
        }

        //注释
        #region 用户登录
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("login")]
        [AllowAnonymous]//允许匿名访问//[FromServices] IWebHostEnvironment env
        public IActionResult Login(UserLogin dto)
        {
            //验证模型规则
            if (!ModelState.IsValid)
            {
                return Ok(new ResponseModel() { Code = State.数据未导入, Message = "数据未导入" });
            }
            var user = _userService.GetLogin(dto);
            if (user != null)
            {
                Dictionary<string, string> keyValuePairs = new Dictionary<string, string>
                {
                        { "LoginName", dto.Name },
                        { "SuperAdmin", "true"} //假定用户属于SuperAdmin角色
                };

                return Ok(new ResponseModel { Code = State.请求成功, Data = user.Data, Message = "用户登录成功", Token = _tokenHelper.CreateToken(keyValuePairs)});
            }
            else
            {
                return Ok(user);
            }
        }
        #endregion

        #region 用户添加
        /// <summary>
        /// 用户添加
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UserAdd(AddUserDto m)
        {
            if (!ModelState.IsValid)
            {
                return Ok(new ResponseModel() { Code = State.数据未导入, Message = "数据未导入" });
            }
            var data = _userService.UserAdd(m);
            if (data.Count > 0)
            {
                return Ok(new ResponseModel() { Code = State.请求成功, Data = data, Message = "添加成功!" });
            }
            else
            {
                return Ok(new ResponseModel() { Code = State.请求成功, Data = data, Message = "账号已经存在,请重新输入!" });
            }
        }
        #endregion

        #region 用户分页显示
        /// <summary>
        /// 用户分页显示
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]//允许匿名访问
        public IActionResult UserQuery(string? UserName, int pageIndex, int pageSize)
        {
            if (pageIndex<=0)
            {
                pageIndex = 1;
            }
            if (pageSize<=0)
                pageSize = 10;

            return Ok(_userService.UserQuery(UserName,pageIndex,pageSize));
        }
        #endregion

        /// <summary>
        /// 用户修改
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UserUpdate(AddUserDto m)
        {
            return Ok(_userService.UserUpdate(m));
        }

        /// <summary>
        /// 角色显示
        /// </summary>
        /// <returns></returns>
        [HttpGet("Role")]
        public IActionResult RoleQuery()
        {
            return Ok(_userService.RoleQuery());
        }

        /// <summary>
        /// 角色信息添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("AddRoles")]
        [AllowAnonymous]//允许匿名访问
        public IActionResult AddUserRole(UserRoleDto dto)
        {
            if (!ModelState.IsValid)
            {
                return Ok(new ResponseModel() { Code = State.数据未导入, Message = "数据未导入" });
            }
            return Ok(_userService.AddUserRole(dto));
        }

        /// <summary>
        /// 用户删除（单删）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult UserDelete(string id)
        {
            var data = _userService.UserDelete(id);
            return Ok(data);
        }

        /// <summary>
        /// 用户删除（批删）
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpDelete("all")]
        public IActionResult AllUserDelete(List<AddUserDto> dto)
        {
            var data = _userService.AllUserDelete(dto);
            return Ok(data);
        }
    }
}
